import { _decorator, Component, Node } from 'cc';
import { App } from '../../../App';
import { Vec3 } from 'cc';
import { Ball } from '../Ball';
import { instantiate } from 'cc';
import { Prefab } from 'cc';
import { Vec2 } from 'cc';
import UtilTool from '../../../tools/UtilTool';
import { view } from 'cc';
import { Camera } from 'cc';
import { find } from 'cc';
import { GameModel2 } from './GameModel2';
import { TrainLayer } from './GameConstant2';
import { loadCommonPreFab } from '../../../tools/PopuUp';
import { TipMg } from '../../../tools/TipMg';
import { Role } from '../Role';
const { ccclass, property } = _decorator;

export default class GameUtils2 {
    
    // 获取游戏资源
    static getRes(path) {
        // @ts-ignore
        let resList = App.Model.gameView.resList

        if (!resList[path]) {
            console.error("资源不存在", path)
            return null
        }
        
        return resList[path]
    }

    // 打印每个球的信息
    static printBallPos() {
        let whiteBall = (App.Model.gameView as GameModel2).whiteBall
        let otherBallList  = App.Model.gameManage.otherBallArr

        let ballList = []

        ballList.push({number:0, x:whiteBall.worldPosition.x, z: whiteBall.worldPosition.z})

        otherBallList.forEach((ball) => {
            if(ball.active) {
                let number = ball.getComponent(Ball).number
                ballList.push({number:number, x:ball.worldPosition.x, z: ball.worldPosition.z})
            }
        })

        let text = JSON.stringify(ballList)
        UtilTool.printLog("test ball list ", text)
    }

    // 实现弹窗
    static async showLayer(layerType : TrainLayer) {

        let layerNameList = [
            'GameTrainFail',
            'GameTrainWin',
            'GameTrainExit',
        ]

        let layerName = layerNameList[layerType]

        if(layerName) {
            let parent = this.getUiParent()
            let node = await loadCommonPreFab("prefabs/game/layer2/"+layerName, parent)
            return node
        } else {
            console.error("layer name is not exist")
            return null
        }

    }

    static getUiParent() : Node {
        return (App.Model.gameView as GameModel2).viewNode
    }

    // 计算两条直线的交点
    static getLineCrossPoint(line1, line2) : Vec2 {
        let a1 = line1[0]
        let b1 = line1[1]
        let c1 = line1[2]

        let a2 = line2[0]
        let b2 = line2[1]
        let c2 = line2[2]

        let x = (b1*c2 - b2*c1) / (a1*b2 - a2*b1)
        let y = (a2*c1 - a1*c2) / (a1*b2 - a2*b1)
        return new Vec2(x, y)
    }

    // 根据两点获取直线方程 ax+by+c=0
    static getLineParms(p1 : Vec2, p2 : Vec2) {
        let a = p2.y - p1.y
        let b = p1.x - p2.x
        let c = p2.x * p1.y - p1.x * p2.y

        return [a, b, c]
    }

    // 求出点到直线的距离
    static getPointToLineDistance(p : Vec2, line) {
        let a = line[0]
        let b = line[1]
        let c = line[2]

        let distance = Math.abs(a*p.x + b*p.y + c) / Math.sqrt(a*a + b*b)
        return distance
    }

    // 获取过一点和直线垂直的直线  // 过点P(x0,y0)与直线ax+by+c=0,
    /** 垂直的方程是 y + x - kx0 - y0 = 0 (其中k = -a/b)
     *  y + x + a/bx0 - y0 = 0, c = a/bx0 - y0
     **/
    static getVerticalLine(pos : Vec2, line) {
        let a = line[0]
        let b = line[1]
        // let c = line[2]

        let c2 = a / b * pos.x - pos.y
        let y2 = -c2 // 带入x=0,求出y
        let pos2 = new Vec2(0, y2)

        return this.getLineParms(pos, pos2)
    }

    // interface Circle {
    //     center: { x: number; y: number };
    //     radius: number;
    //   }
    
    // 圆与直线的交点
    static getLineCircleIntersectionPoints(a: number, b: number, c: number, circle: any) {
        // 计算一般式中的常数 k
        const k = -c / (a * a + b * b);
        
        // 将一般式转换为标准式，即 y = mx + n
        const m = -a / b;
        const n = -c / b;
        
        // 计算圆心到直线的距离
        const distance = Math.abs(k * Math.sqrt(a * a + b * b));
        
        if (distance > circle.radius) {
            // 直线与圆没有交点
            return [];
        } else if (distance === circle.radius) {
            // 直线与圆相切，只有一个交点
            const intersection = {
                x: -a * c / (a * a + b * b),
                y: -b * c / (a * a + b * b),
            };
            return [intersection];
        } else {
            // 直线与圆有两个交点
            const x0 = -a * c / (a * a + b * b);
            const y0 = -b * c / (a * a + b * b);
            const d = Math.sqrt(Math.pow(circle.radius, 2) - Math.pow(distance, 2));
            const mult = Math.sqrt(1 / (1 + Math.pow(m, 2)));
            const x1 = x0 + mult * d;
            const x2 = x0 - mult * d;
            const y1 = y0 + m * mult * d;
            const y2 = y0 - m * mult * d;
            return [{ x: x1, y: y1 }, { x: x2, y: y2 }];
        }
    }

}

